Input and Output Functions in C
Input and Output functions in C are a set of functions provided by the C standard library for performing input and output operations. These functions allow you to read data from various sources and write data to various destinations. Input functions are used to obtain data from external sources, such as the keyboard or a file, while output functions are used to display or save data to external destinations, like the screen or a file.
Output Function:
The printf() function is a versatile and widely used output function in C. It allows you to format and display data on the screen or another output stream. printf() uses format specifiers to control the formatting of the output and can display various data types, such as integers, floating-point numbers, characters, and strings.
int main()
{
int age = 18;
printf("The age is %d\n",age);
return 0;
}
Other commonly used output functions in C include:
1. putchar: Writes a single character to the standard output (usually the screen)
2. puts(): Writes a string to the standard output followed by a newline character.
3. fputs(): Writes a string to a file or the standard output.
4. fprintf(): Writes formatted data to a specified file.
These output functions give you the ability to present information and results to users, create log files, or generate formatted output for various purposes in C programming.
Input Function
The scanf() function is a popular and versatile input function in C. It is used for formatted input, allowing you to read data from the standard input (usually the keyboard) or other input sources. scanf() uses format specifiers to specify the type and format of the data being read.
int main()
{
int age;
printf("Enter a number: ";
scanf("%d ",&age);
printf("Your age is: %d",age);
return 0;
}
Other commonly used output functions in C include:
1. getchar: Reads a single character from the standard input.
2. fgets(): Reads a line of text from the standard input or a specified file.
3. fscanf(): Reads formatted data from a specified file.
These input functions allow your program to interact with users, read data from files, and process information from various sources. They are vital for building applications that need to accept external input.